package com.glory.wxelectricitypda.bean;

import android.content.Context;
import android.support.v4.app.FragmentManager;
import android.widget.Toast;

import com.glory.wxelectricitypda.base.App;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_CommonCallback;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralFragmentDialog;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DialogUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_ToastUtils;

import java.io.Serializable;
import java.util.ArrayList;

/**
 * @author lx
 * @date 2019/3/22
 * @desc
 */
public class ResponseBean<T> implements Serializable {
    private ResponseHeaderBean Header;
    private ResponseReturnBean Return;
    private T Body;
    private transient FreeUI_GeneralFragmentDialog tipDialog;

    //获取到对象后，必须要判断是否获取成功
    public boolean isSuccess(Context context, boolean isHaveBody) {
        if (Return.getReturnCode().equals("200")) {
            if (isHaveBody) {
                if (Body == null) {
                    FreeApi_ToastUtils.showFreeToast("暂无请求内容", context, false, Toast.LENGTH_SHORT);
                    return false;
                } else {
                    if (Body instanceof ArrayList) {
                        if (((ArrayList) Body).size() > 0) {
                            return true;
                        } else {
                            FreeApi_ToastUtils.showFreeToast("暂无请求数据", context, false, Toast.LENGTH_SHORT);
                            return false;
                        }
                    } else {
                        return true;
                    }
                }
            } else {
                return true;
            }
        } else {
            FreeApi_ToastUtils.showFreeToast(Return.getReturnMessage(), context, false, Toast.LENGTH_SHORT);
            return false;
        }
    }

    //获取到对象后，必须要判断是否获取成功
    public boolean isSuccess(Context context, boolean isHaveBody, FragmentManager supportFragmentManager) {
        if (Return.getReturnCode().equals("200")) {
            if (isHaveBody) {
                if (Body == null) {
                    FreeApi_ToastUtils.showFreeToast("暂无请求内容", context, false, Toast.LENGTH_SHORT);
                    return false;
                } else {
                    if (Body instanceof ArrayList) {
                        if (((ArrayList) Body).size() > 0) {
                            return true;
                        } else {
                            FreeApi_ToastUtils.showFreeToast("暂无请求数据", context, false, Toast.LENGTH_SHORT);
                            return false;
                        }
                    } else {
                        return true;
                    }
                }
            } else {
                return true;
            }
        } else {
            showFailDialog(Return.getReturnMessage(), supportFragmentManager);
//            FreeApi_ToastUtils.showFreeToast(Return.getReturnMessage(), context, false, Toast.LENGTH_SHORT);
            return false;
        }
    }



    public T getBody() {
        return Body;
    }

    public void setBody(T body) {
        Body = body;
    }

    public ResponseHeaderBean getHeader() {
        return Header;
    }

    public void setHeader(ResponseHeaderBean header) {
        Header = header;
    }

    public ResponseReturnBean getReturn() {
        return Return;
    }

    public void setReturn(ResponseReturnBean aReturn) {
        Return = aReturn;
    }

    public class ResponseReturnBean {
        private String ReturnCode;
        private String ReturnMessage;

        public String getReturnCode() {
            return ReturnCode == null ? "" : ReturnCode;
        }

        public void setReturnCode(String returnCode) {
            ReturnCode = returnCode;
        }

        public String getReturnMessage() {
            return ReturnMessage == null ? "" : ReturnMessage;
        }

        public void setReturnMessage(String returnMessage) {
            ReturnMessage = returnMessage;
        }
    }

    private void showFailDialog(String message, FragmentManager fragmentManager) {

        tipDialog = FreeApi_DialogUtils.getConfirmDialog(App.getInstance().app, "确定", message, new FreeUI_CommonCallback() {
            @Override
            public void onSuccess() {

            }
        });
        tipDialog.show(fragmentManager, "DIALOG");
    }

}
